package com.zhl.media.app.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;

import com.jess.arms.di.component.AppComponent;
import com.orhanobut.hawk.Hawk;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhl.media.app.R;
import com.zhl.media.app.di.component.DaggerFrameComponent;
import com.zhl.media.app.di.module.FrameModule;
import com.zhl.media.app.mvp.adapter.HmListViewAdapter;
import com.zhl.media.app.mvp.contract.FrameContract;
import com.zhl.media.app.mvp.presenter.FramePresenter;
import com.zhl.media.commonres.base.AppFragment;
import com.zhl.media.commonres.view.EmptyLayout;
import com.zhl.media.commonsdk.entity.Hm;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.github.douglasjunior.androidSimpleTooltip.SimpleTooltip;

public class FrameFragment extends AppFragment<FramePresenter> implements FrameContract.View{
    @BindView(R.id.expandableListView)
    ExpandableListView expandableListView;
    @BindView(R.id.emptyLayout)
    EmptyLayout emptyLayout;
    List<Hm> mData;
    private HmListViewAdapter adapter;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @BindView(R.id.content)
    LinearLayout content;
    private String date;

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerFrameComponent
                .builder()
                .appComponent(appComponent)
                .frameModule(new FrameModule(this))
                .build()
                .inject(this);
    }
    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frame_fragment, container, false);
        return view;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        refreshLayout.setEnableLoadMore(false);
        mData = new ArrayList<>();
        adapter = new HmListViewAdapter(getActivity(),mData);
        expandableListView.setAdapter(adapter);
        date = Hawk.get("thisDate","0");
        refreshLayout.autoRefresh();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout1) {
                if(!date.equals("0")){
                    mPresenter.getGghm(date);
                }else {
                    emptyLayout.bindView(refreshLayout);
                    emptyLayout.showEmpty("~暂时未获取本期日期~");
                }
            }
        });
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                String name = mData.get(groupPosition).getProjects().get(childPosition).getName();
                String zl = mData.get(groupPosition).getProjects().get(childPosition).getZl();
                String ph = mData.get(groupPosition).getProjects().get(childPosition).getPh();
                String th = mData.get(groupPosition).getProjects().get(childPosition).getTh();
                new SimpleTooltip.Builder(getActivity())
                        .anchorView(v)
                        .text(name + "\n" + "总量：" + zl + "，普画：" + ph + "，贴画：" + th)
                        .gravity(Gravity.TOP)
                        .animated(true)
                        .textColor(getResources().getColor(R.color.public_white))
                        .backgroundColor(getResources().getColor(R.color.public_color_FF53A8F4))
                        .transparentOverlay(true)
                        .build()
                        .show();
                return false;
            }
        });
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    public static FrameFragment newInstance() {
        Bundle args = new Bundle();
        FrameFragment fragment = new FrameFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {
        refreshLayout.finishRefresh();
    }
    @Override
    public void showMessage(@NonNull String message) {

    }
    @Override
    public void launchActivity(@NonNull Intent intent) {

    }
    @Override
    public void killMyself() {

    }
    @Override
    public void onList(List<Hm> list) {
        mData.clear();
        mData.addAll(list);
        if(mData.size()==0){
            emptyLayout.bindView(content);
            emptyLayout.showEmpty("~暂无广告画面~");
        }else {
            emptyLayout.hideEmpty();
        }
        adapter.notifyDataSetChanged();
    }
}
